package com.paw.common.domain;

import java.util.List;

import cn.hutool.core.net.NetUtil;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.paw.common.enums.ErrorShowTypeEnum;
import com.paw.common.enums.ICommonEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author Rubble
 * @date 2021/8/14
 * 参见：https://pro.ant.design/zh-CN/docs/request/
 **/
@Data
@ApiModel("Response")
public class Result<T> {

  public static int SUCCESS_CODE = 200;
  public static String SUCCESS_MSG = "success";

  @ApiModelProperty("成功")
  private Boolean success;

  @ApiModelProperty("数据")
  private T data;

  @ApiModelProperty("编码")
  private int code;

  @ApiModelProperty("消息")
  private String message;

  /**
   * 错误显示类型
   * error display type： 0 silent; 1 message.warn; 2 message.error; 4 notification; 9 page
   * @see ErrorShowTypeEnum
   */
  @ApiModelProperty("错误显示类型")
  private Integer showType = 2;

  @ApiModelProperty("请求跟踪ID")
  private String traceId;

  @ApiModelProperty("机器")
  private String host;


  @ApiModelProperty("总记录数")
  @JsonSerialize(using = ToStringSerializer.class)
  private Long total;


  public  static <T> Result<T> success(T data){
    Result<T> result = new Result<>();
    result.setCode(SUCCESS_CODE);
    result.setMessage(SUCCESS_MSG);
    result.setData(data);
    return result;
  }
  public  static <T extends List<M>,M> Result<T> page (T data,Long total){
    Result<T> result = new Result<>();
    result.setCode(SUCCESS_CODE);
    result.setMessage(SUCCESS_MSG);
    result.setData(data);
    result.setTotal(total);
    return result;
  }


  public static <T> Result<T> error(Integer errorCode,String errorMessage){
    return Result.error(errorCode,errorMessage, ErrorShowTypeEnum.ERROR_MESSAGE);
  }

  public static <T> Result<T> error(Integer errorCode,String errorMessage, ErrorShowTypeEnum showTypeEnum){
    Result<T> result = new Result<>();
    result.setCode(errorCode);
    result.setMessage(errorMessage);
    result.setShowType(showTypeEnum.getCode());
    return result;
  }

  public static <T> Result<T> generate(ICommonEnum commonEnum){
    Result<T> result = new Result<>();
    result.setCode(commonEnum.getCode());
    result.setMessage(commonEnum.getDesc());
    return result;
  }

  public static <T> Result<T> generate(Integer code,String message, T data){
    Result<T> result = new Result<>();
    result.setCode(code);
    result.setMessage(message);
    result.setData(data);
    return result;
  }

  public Boolean getSuccess () {
    success = code==200;
    return success;
  }
}
